﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;

namespace ThuongMaiDienTuFinally.Shared
{
    public partial class Main : System.Web.UI.MasterPage
    {
        DBDataContext db = new DBDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[Column.User] == null)
            {
                login_box.Visible = true;
                logout_box.Visible = false;
            }
            else 
            {
                login_box.Visible = false;
                logout_box.Visible = true;
            }
            login_box.Visible = false;
            logout_box.Visible = true;

        }

        protected void bnt_login_Click(object sender, EventArgs e)
        {
            string key = System.Configuration.ConfigurationManager.AppSettings["KeySecurity"].ToString();
            string username = Server.HtmlEncode(txt_username.Text);
            string password = Server.HtmlEncode(txt_pass.Text);

            var user = (from c in db.Tb_Users
                        where c.UserName == username
                        select new { c }).SingleOrDefault();


            if (user != null)
            {
                if (algorithmPassword.Encode(password, key) == user.c.MatKhau)
                {
                    Session[Column.User] = user;
                }
                else
                {
                    Server.Transfer(PageName.HomePage);
                }
            }
        }
    }
}